package cn.lml.lession.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author  日期工具
 */
public class DateUtils {
	private static String[] parsePatterns = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "yyyy-MM",
			"yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM/dd+HH:mm", "yyyy/MM/dd", "yyyy/MM", "yyyyMMddHHmmss",
			"yyyyMMddHHmm", "yyyyMMdd", "yyyyMM" };

	/**
	 * @param date
	 * @param pattern
	 * @return 得到日期字符串 默认格式（yyyy/MM/dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss"
	 */
	public static String formatDate(Date date, String... pattern) {
		if (date == null)
			return null;
		String p = "yyyy/MM/dd";
		if (pattern != null && pattern.length > 0) {
			p = pattern[0];
		} 
		SimpleDateFormat sf = new SimpleDateFormat(p);
		return sf.format(date);
	}
	
	/**
	 * @param pattern
	 * @return 得到当前日期字符串 默认格式（yyyy/MM/dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss"
	 */
	public static String formatNowDate(String... pattern) {
		return formatDate(new Date(), pattern);
	}

	
	/**
	 * @param dateStr
	 * @param pattern
	 * @return 解析字符串日期
	 */
	public static Date parseDate(String dateStr, String... pattern) {
		if (dateStr == null || dateStr.equals(""))
			return null;
		String p = null;
		if (pattern != null && pattern.length > 0) {
			p = pattern[0].toString();
		} else {
			p = parsePatterns[0];
		}
		SimpleDateFormat sf = new SimpleDateFormat(p);
		try {
			return sf.parse(dateStr);
		} catch (ParseException e) {
			if (pattern == null)
				return parseDate(dateStr, new String[] { parsePatterns[1] });
			for (int i = 0; i < parsePatterns.length - 1; i++) {
				if (parsePatterns[i].equals(p)) 
					return parseDate(dateStr, new String[] { parsePatterns[i + 1] });
				if (i == parsePatterns.length - 2 && !p.equals(parsePatterns[i + 1])) {
					return parseDate(dateStr, new String[] { parsePatterns[0] });
				}
			}
		}
		return null;
	}

	/**
	 * @param dateStr
	 * @param pattern
	 * @return 解析字符串日期 返回时间戳
	 */
	public static Long parseDateToLong(String dateStr, String... pattern) {
		if (dateStr == null || dateStr.equals(""))
			return null;
		Date date=parseDate(dateStr, pattern);
		if(date!=null) return date.getTime();
		return null;
	}

	/**
	 * @param dateStr
	 * @param pattern
	 * @return 解析字符串日期,返回后一天
	 */
	public static Date nextDate(String dateStr, String... pattern) {
		Date date = parseDate(dateStr, pattern);
		if (date != null)
			return new Date(date.getTime() + 24 * 60 * 60 * 1000);
		return null;
	}

	/**
	 * @param dateStr
	 * @param pattern
	 * @return 解析字符串日期,返回前一天
	 */
	public static Date prevDate(String dateStr, String... pattern) {
		Date date = parseDate(dateStr, pattern);
		if (date != null)
			return new Date(date.getTime() - 24 * 60 * 60 * 1000);
		return null;
	}
	
	/**
	 * 计算两日期相隔天数
	 * @param aDate
	 * @param bDate
	 * @return
	 */
	public static long dateDays(Date aDate, Date bDate) {
		long myTime;
		long myTime2;
		long days = 0;
		myTime = (aDate.getTime() / 1000);
		myTime2 = (bDate.getTime() / 1000);
		if (myTime > myTime2) {
			days = (myTime - myTime2) / (1 * 60 * 60 * 24);
		} else {
			days = (myTime2 - myTime) / (1 * 60 * 60 * 24);
		}
		return days;

	}
}
